import 'dart:convert';
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:whale/helper/utils/color.dart';
import 'package:whale/helper/utils/httputils/http_man.dart';
import 'package:whale/helper/utils/util.dart';
import 'package:dio/src/form_data.dart' as fromdata;
import '../../../helper/utils/httputils/urlpath.dart';
import 'package:dio/dio.dart' as dio;
import 'model/collect.dart';
import 'model/upmodel.dart';
import 'state.dart';

class My_uploadLogic extends GetxController with GetSingleTickerProviderStateMixin{
  final My_uploadState state = My_uploadState();
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    state.titles = ['全部', '政策措施', '招标采购'];
    state.tabCtrl = TabController(length: state.titles.length, vsync: this);
    getList();
  }


  void showBottom(){
    Get.bottomSheet(
      Container(
        height: 200.r,
        padding: EdgeInsets.all(10.r),
        margin: EdgeInsets.all(12.r),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6.r)  ,
          color: Colors.white
        ),
        child: Column(
          children: [

            Stack(
              children: [
                Align(
                  alignment: Alignment.center,
                  child: Container(
                    margin: EdgeInsets.only(top: 15.r),
                    child: Text('选择上传文件类型',style: TextStyle(color: ColorsUtil.title,fontSize: 15.r,fontWeight: FontWeight.bold),),
                  ),
                ),
                Align(
                  alignment: Alignment.topRight,
                  child: Image.asset('icon_close.png'.imgPathMe,width: 23.r,height: 23.r,),
                )
              ],
            ),
            SizedBox(height: 20.r,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                GestureDetector(
                  onTap: ()=>pickFile('政策措施'),
                  child: Container(
                    width: 125.r,
                    height: 100.r,
                    decoration: BoxDecoration(
                        color: const Color(0xffF6FAFF),
                      borderRadius: BorderRadius.circular(4.r)
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset('up_bg2.png'.imgPathMe,width: 63.r,height: 44.r,),
                        SizedBox(height: 12.r,),
                        Text('政策措施',style: TextStyle(color: ColorsUtil.title,fontSize: 12.r),)
                      ],
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: ()=>pickFile('招标采购'),
                  child: Container(
                    width: 125.r,
                    height: 100.r,
                    decoration: BoxDecoration(
                        color: Color(0xffF0FAF7),
                        borderRadius: BorderRadius.circular(4.r)
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset('up_bg1.png'.imgPathMe,width: 63.r,height: 44.r,),
                        SizedBox(height: 12.r,),
                        Text('招标采购',style: TextStyle(color: ColorsUtil.title,fontSize: 12.r),)
                      ],
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      )
    );
  }

  void getList({String? reportType}) async{
    var res = await MeApi.cloudCOLLECT(reportType??'');
    if (res.isSuccess) {
      List records = res.data['records'];
      state.docs = records.map((e) => Upmodel.fromJson(e)).toList();
      update();
    }
  }
  void uploadFile(String path, {String? name}) async{


    var res =  await HttpMan.instance.upload(
        UrlPath.baseUrl+UrlPath.uploadFiles,
        data: fromdata.FormData.fromMap(
            {'files': [dio.MultipartFile.fromFileSync(path)]}),
        onSendProgress: (int a,int b){
          print('---onSendProgress---$a -- $b');
          // EasyLoading.showProgress(a/b);
        },
        onReceiveProgress: (int a,int b){

        }
    );

      List files = res.data;

      var saveRes =  await MeApi.saveGsFiles({
        'files': files,
        'reportType': state.typeStr,
        // 'province':'',
        // 'city':'',
        // 'industryType':'',
        // 'companyName':'',
        // 'publishTime':'',
        // 'indexNumber':'',
        // 'documentTag':'',
        // 'documentNum':'',
        // 'genre':'',
      });
      if(saveRes.isSuccess){
        getList();
        Future.delayed(Duration(seconds: 5),(){
          getList();
        });
      }else{
        EasyLoading.showToast(res.msg);
      }
      // fromdata.FormData data = await fromdata.FormData.fromMap(
      //     {'files': [dio.MultipartFile.fromFileSync(path)]});
      // var res = await HttpMan.instance.singleFile(data);

  }



  pickFile(String typeStr) async{
    state.typeStr = typeStr;
    Get.back();
    FilePickerResult? result = await FilePicker.platform.pickFiles();

    if (result != null) {
      File file = File(result.files.single.path!);
      uploadFile(file.path);
    }
  }
}
